package com.atguigu.medicalsugar.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.medicalsugar.bean.*;
import com.atguigu.medicalsugar.service.PatientService;
import com.atguigu.medicalsugar.service.TradeService;
import com.atguigu.medicalsugar.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author lzc
 * @Date 2023/5/29 10:15
 */
@RestController
public class SugarController {
    @Autowired
    UserService userService;
    
    @RequestMapping("/sugar/user")
    public String user(Integer date) {
        if (date == null) { // 如果没有传入日期,则使用今天做日期
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
        List<UserBean> list = userService.user(date);
        
        JSONObject result = new JSONObject();
        
        result.put("status", 0);
        result.put("msg", "");
        JSONObject data = new JSONObject();
        data.put("total", list.size());
        
        JSONArray columns = new JSONArray();
        JSONObject c1 = new JSONObject();
        c1.put("name", "日期");
        c1.put("id", "date");
        columns.add(c1);
        JSONObject c2 = new JSONObject();
        c2.put("name", "新增用户数");
        c2.put("id", "new_user_ct");
        columns.add(c2);
        JSONObject c3 = new JSONObject();
        c3.put("name", "活跃用户数");
        c3.put("id", "active_user_ct");
        columns.add(c3);
        JSONObject c4 = new JSONObject();
        c4.put("name", "激活用户数");
        c4.put("id", "new_active_user_ct");
        columns.add(c4);
        data.put("columns", columns);
        
        JSONArray rows = new JSONArray();
        for (UserBean userBean : list) {
            JSONObject row = new JSONObject();
            row.put("date", userBean.getDate() + "");
            row.put("new_user_ct", userBean.getNew_user_ct());
            row.put("active_user_ct", userBean.getActive_user_ct());
            row.put("new_active_user_ct", userBean.getNew_active_user_ct());
            rows.add(row);
        }
        data.put("rows", rows);
        
        result.put("data", data);
        return result.toJSONString();
    }
    
    @Autowired
    PatientService patientService;
    
    @RequestMapping("/sugar/patient")
    public String patient(Integer date) {
        if (date == null) { // 如果没有传入日期,则使用今天做日期
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
        List<PatientBean> list = patientService.patient(date);
        
        JSONObject result = new JSONObject();
        
        result.put("status", 0);
        result.put("msg", "");
        JSONObject data = new JSONObject();
        data.put("total", list.size());
        
        JSONArray columns = new JSONArray();
        JSONObject c1 = new JSONObject();
        c1.put("name", "日期");
        c1.put("id", "date");
        columns.add(c1);
        JSONObject c2 = new JSONObject();
        c2.put("name", "新增医生数");
        c2.put("id", "register_ct");
        columns.add(c2);
        JSONObject c3 = new JSONObject();
        c3.put("name", "活跃医生数");
        c3.put("id", "active_ct");
        columns.add(c3);
        JSONObject c4 = new JSONObject();
        c4.put("name", "激活医生数");
        c4.put("id", "new_active_ct");
        columns.add(c4);
        data.put("columns", columns);
        
        JSONArray rows = new JSONArray();
        for (PatientBean patientBean : list) {
            JSONObject row = new JSONObject();
            row.put("date", patientBean.getDate() + "");
            row.put("register_ct", patientBean.getRegister_ct());
            row.put("active_ct", patientBean.getActive_ct());
            row.put("new_active_ct", patientBean.getNew_active_ct());
            rows.add(row);
        }
        data.put("rows", rows);
        
        result.put("data", data);
        return result.toJSONString();
    }
    
    
    @Autowired
    TradeService tradeService;
    
    @RequestMapping("/sugar/trade/gmv")
    public String trade(Integer date) {
        if (date == null) { // 如果没有传入日期,则使用今天做日期
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
        
        List<TradeGmvBean> list = tradeService.gmv(date);
        
        JSONObject result = new JSONObject();
        
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
        
        JSONArray categories = new JSONArray();
        for (TradeGmvBean tradeGmvBean : list) {
            categories.add(tradeGmvBean.getName());
        }
        data.put("categories", categories);
        
        JSONArray series = new JSONArray();
        
        JSONObject obj = new JSONObject();
        obj.put("name", "交易金额");
        JSONArray objData = new JSONArray();
        for (TradeGmvBean tradeGmvBean : list) {
            objData.add(tradeGmvBean.getAmount());
        }
        obj.put("data", objData);
        series.add(obj);
        
        
        data.put("series", series);
        
        result.put("data", data);
        return result.toJSONString();
    }
    
    @RequestMapping("/sugar/trade/prescription/patient_sex")
    public String prescriptionByPatientSex(Integer date) {
        if (date == null) { // 如果没有传入日期,则使用今天做日期
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
        
        
        List<TradePatientSexBean> list = tradeService.prescriptionAmountByPatientSex(date);
        JSONObject result = new JSONObject();
        
        result.put("status", 0);
        result.put("msg", "");
        
        JSONArray data = new JSONArray();
        
        for (TradePatientSexBean bean : list) {
            JSONObject obj = new JSONObject();
            obj.put("name", bean.getPatient_sex());
            obj.put("value", bean.getAmount());
            data.add(obj);
        }
        result.put("data", data);
        return result.toJSONString();
    }
    
    @RequestMapping("/sugar/trade/patient/amount")
    public String patientAmount(Integer date) {
        if (date == null) { // 如果不传日期使用今天作为查询的日期
            date = Integer.valueOf(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
        
        
        List<TradePatientAgeRangeBean> list = tradeService.amountByPatientAgeRange(date);
        System.out.println(list);
        Map<Long, TradePatientAgeRangeBean> map = new HashMap<>();
        for (TradePatientAgeRangeBean bean : list) {
            map.put(bean.getAge_range(), bean);
        }
        
        
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
        
        JSONArray categories = new JSONArray();
        categories.addAll(Arrays.asList(TradePatientAgeRangeBean.ageRangeDescs));
        data.put("categories", categories);
        
        JSONArray series = new JSONArray();
        
        JSONObject c = new JSONObject();
        c.put("name", "问诊金额");
        JSONArray cData = new JSONArray();
        c.put("data", cData);
        series.add(c);
        
        JSONObject cPay = new JSONObject();
        cPay.put("name", "问诊支付金额");
        JSONArray cPayData = new JSONArray();
        cPay.put("data", cPayData);
        series.add(cPay);
        
        JSONObject p = new JSONObject();
        p.put("name", "开处方金额");
        JSONArray pData = new JSONArray();
        p.put("data", pData);
        series.add(p);
        
        JSONObject pPay = new JSONObject();
        pPay.put("name", "开处方支付金额");
        JSONArray pPayData = new JSONArray();
        pPay.put("data", pPayData);
        series.add(pPay);
        
        data.put("series", series);
        
        for (long ageRange = 0; ageRange < 11; ageRange++) {
            
            TradePatientAgeRangeBean bean = map.getOrDefault(ageRange,
                                                             TradePatientAgeRangeBean.builder()
                                                                 .age_range(ageRange)
                                                                 .consultation_amount(new BigDecimal(0))
                                                                 .consultation_pay_amount(new BigDecimal(0))
                                                                 .prescription_amount(new BigDecimal(0))
                                                                 .prescription_pay_amount(new BigDecimal(0))
                                                                 .build()
            );
            cData.add(bean.getConsultation_amount());
            cPayData.add(bean.getConsultation_pay_amount());
            pData.add(bean.getPrescription_amount());
            pPayData.add(bean.getPrescription_pay_amount());
        }
        result.put("data", data);
        
        return result.toJSONString();
        
    }
    
    
    @RequestMapping("/sugar/trade/hospital/amount")
    public String hostpitalAmount(Integer date) {
        if (date == null) { // 如果没有传入日期,则使用今天做日期
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
        List<TradeHostpitalBean> list = tradeService.statsByHostpital(date);
    
        JSONObject result = new JSONObject();
        
        result.put("status", 0);
        result.put("msg", "");
        JSONObject data = new JSONObject();
        data.put("total", list.size());
        
        JSONArray columns = new JSONArray();
        JSONObject c1 = new JSONObject();
        c1.put("name", "医院");
        c1.put("id", "hospital");
        columns.add(c1);
        
        JSONObject c2 = new JSONObject();
        c2.put("name", "问诊金额");
        c2.put("id", "c_amount");
        columns.add(c2);
        
        JSONObject c3 = new JSONObject();
        c3.put("name", "问诊数量");
        c3.put("id", "c_ct");
        columns.add(c3);
        
        JSONObject c4 = new JSONObject();
        c4.put("name", "处方金额");
        c4.put("id", "p_amount");
        columns.add(c4);
        
        JSONObject c5 = new JSONObject();
        c5.put("name", "处方数量");
        c5.put("id", "p_ct");
        columns.add(c5);
        
        data.put("columns", columns);
        System.out.println(list);
        JSONArray rows = new JSONArray();
        for (TradeHostpitalBean userBean : list) {
            JSONObject row = new JSONObject();
            row.put("hospital", userBean.getHospital());
            row.put("c_amount", userBean.getConsultation_amount());
            row.put("c_ct", userBean.getConsultation_ct());
            row.put("p_amount", userBean.getPrescription_amount());
            row.put("p_ct", userBean.getPrescription_ct());
            rows.add(row);
        }
        data.put("rows", rows);
        
        result.put("data", data);
        return result.toJSONString();
    }
}
